package davywong.Acitivity_02;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import android.net.Uri;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Activity_02Activity extends Activity {
	
	// alarm manager
	private AlarmManager alarmManager;
	private PendingIntent pendingIntent;
	
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // add TextView and Button
        TextView myTextView = (TextView)findViewById(R.id.myTextView);
        Button myButton = (Button)findViewById(R.id.myButton);
        myTextView.setText("this is the first activity");
        myButton.setText("go to another activity" + "\n" + "2nd line");
        
        // bind listener to button
        myButton.setOnClickListener(new MyButtonListener());
        
        
        // sms button
        Button mySMSButton = (Button)findViewById(R.id.mySMSButton);
        mySMSButton.setText("send sms to 13928418844");
        mySMSButton.setOnClickListener(new MySMSButtonListener());
        
        // web button
        Button myWebButton = (Button)findViewById(R.id.myWebButton);
        myWebButton.setText("go to default web");
        myWebButton.setOnClickListener(new MyWebButtonListener());
        
        
        // alarm manager
        alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmService.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        alarmManager.setTimeZone("GMT+08:00");
        
        
    }    
 
    // this is an inner class, a listener
    class MyButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			// create an intent object
			Intent intent = new Intent();
			
			// put 123 into testIntent
			// this is a way to deliver value using "key value"
			intent.putExtra("testIntent", "123");
			
			// jump from activity02 to other activity
			intent.setClass(Activity_02Activity.this, OtherActivity.class);
			Activity_02Activity.this.startActivity(intent);
		
		}
    	
    }
    class MySMSButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
		    Uri uri = Uri.parse("smsto:13928418844");
		    Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
		    smsIntent.putExtra("sms_body", "The SMS Text");
		    startActivity(smsIntent);			
		}
    	
    }
    class MyWebButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
					System.currentTimeMillis() + (5*1000), 
					(5*1000), //interval, 
					pendingIntent);
			
			Uri uri = Uri.parse("http://3g.sina.cn");
			Intent webIntent = new Intent(Intent.ACTION_VIEW, uri);			
			startActivity(webIntent);
		}
    	
    }
    
}